package net.xdclass.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.controller.request.LinkGroupAddRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.manager.LinkGroupManager;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.mapper.LinkGroupMapper;
import net.xdclass.model.vo.LinkGroupVO;
import net.xdclass.service.ILinkGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hr
 * @since 2025-08-04
 */
@Service
@Slf4j
public class LinkGroupServiceImpl extends ServiceImpl<LinkGroupMapper, LinkGroupDO> implements ILinkGroupService {

    @Autowired
    private LinkGroupManager linkGroupManager;
    @Override
    public int add(LinkGroupAddRequest request) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = LinkGroupDO.builder()
                .title(request.getTitle())
                .accountNo(accountNo)
                .build();
        int rows = linkGroupManager.add(linkGroupDO);

        return rows;
    }

    @Override
    public int del(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        return linkGroupManager.del(groupId,accountNo);
    }

    @Override
    public LinkGroupVO detail(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId,accountNo);
        LinkGroupVO linkGroupVO = LinkGroupVO.builder().build();
        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> listAllGroup() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList = linkGroupManager.listAllGroup(accountNo);

        List<LinkGroupVO> linkGroupVOList = BeanUtil.copyToList(linkGroupDOList, LinkGroupVO.class);

        return linkGroupVOList;
    }

    @Override
    public int updateById(LinkGroupUpdateRequest request) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = LinkGroupDO.builder()
                .accountNo(accountNo)
                .title(request.getTitle())
                .id(request.getId())
                .build();
        int rows = linkGroupManager.updateById(linkGroupDO);
        return rows;
    }
}
